/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/
#include "../gpsmapviewmathematic.h"
#include <math.h>
#include <QStringList>

using namespace greenhills::gps;
CGPSPosition CGpsmapViewMathematic::mMyPosition;
CGPSPosition CGpsmapViewMathematic::mLeftUpCorner;
double CGpsmapViewMathematic::mXpixels;
double CGpsmapViewMathematic::mYpixels;
double CGpsmapViewMathematic::mXspace;
double CGpsmapViewMathematic::mYspace;
double CGpsmapViewMathematic::mWidth;
double CGpsmapViewMathematic::mHeight;
double CGpsmapViewMathematic::mScale;

CGpsmapViewMathematic::CGpsmapViewMathematic(QObject * pParent):QObject(pParent)
{
    mMyPosition = CGPSPosition();
    mLeftUpCorner = CGPSPosition();
    mYpixels = 0.0;
    mXpixels = 0.0;
    mYspace = 0.0;
    mXspace = 0.0;
    mWidth = 0.0;
    mHeight = 0.0;
    mScale =(double)(640*3)/(200*29);
}

QPointF CGpsmapViewMathematic::coordToPixmap(CGPSPosition &pPoint)
{    
    double angle =((double) mMyPosition.directionTo(pPoint));
    //double revert = 1;
    //if((angle<=360.0 && angle>=270) || (angle<=180.0 && angle>=90.0))revert =-1;
    angle = (90-angle)/180*M_PI;
    double distance = mMyPosition.distanceTo(pPoint);
    distance = distance*mScale;
    double sx = cos(angle)*distance;//*revert;
    double sy = -sin(angle)*distance;//*revert;
    QPointF point = QPointF(sx,sy);
    return point;
}

QPointF CGpsmapViewMathematic::evaluateDifference(CGPSPosition &pPoint)
{
   QPointF point = coordToPixmap(pPoint);
   point.setX(point.x()+ mXpixels);
   point.setY(point.y()+ mYpixels);
    mXpixels=point.x();
    mYpixels=point.y();
   if(needRedraw()){Q_EMIT redrawNeed(&point);resetPixels();}
   else return point;
}

QString CGpsmapViewMathematic::toGoogleRepresentation(QString &pString)
{
   QStringList lst = pString.split('\260');

   bool ok;
   double stp = lst.at(0).toDouble(&ok);
   double min = lst.at(1).toDouble(&ok);   
            min = min/60;
   if(!ok) return lst.at(0);
   double uprava =stp + min;

   QString ret = QString("%1").arg(uprava,0,'f',7);

   return ret;
}

void CGpsmapViewMathematic::resetPixels()
{
    mXpixels = 0.0;
    mYpixels =0.0;
}

void CGpsmapViewMathematic::myPositionChange(QString &pLon , QString &pLat)
{
    mMyPosition = CGPSPosition(CGPSLatitude().fromString(pLat+"N"),
                                     CGPSLongitude().fromString(pLon+"E"));    
}

void CGpsmapViewMathematic::leftUpCornerChange()
{
    mLeftUpCorner = mMyPosition.positionTo(315,200*29/6*sqrt(2));    
}

void CGpsmapViewMathematic::changeProportions(double pWidth, double pHeight)
{
    mWidth = pWidth;
    mHeight = pHeight;
}

bool CGpsmapViewMathematic::needRedraw()
{
    mXspace += mXpixels;
    mYspace += mYpixels;
    if((640 - mWidth)/2 <= abs(mXspace)){ mXspace=0.0; mYspace=0.0; return true;}
    if((640 - mHeight)/2 <= abs(mYspace)){ mXspace=0.0; mYspace=0.0; return true;}    
    return false;
}
